package soar.template.springboot.service.client.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import soar.template.springboot.service.rmi.hello.CreateAccountRequest;
import soar.template.springboot.service.rmi.hello.CreateAccountResponse;
import soar.template.springboot.service.rmi.hello.Hello;

@Controller
@RequestMapping("/client")
public class HelloController
{
    @Autowired
    @Qualifier("helloProxy")
    private Hello hello;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String index()
    {
        return String.format("你好！ local @ %s", DateTimeFormatter
                .ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()));
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test(@RequestParam(defaultValue = "arron") String echo)
    {
        return hello.test(echo);
    }

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    @ResponseBody
    public Greeting greeting(@RequestParam(defaultValue = "world") String name,
            @RequestParam(defaultValue = "30") int age)
    {
        CreateAccountRequest request = new CreateAccountRequest();
        CreateAccountResponse response = hello.createAccount(request);
        String time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .format(LocalDateTime.now());
        String content = String.format("Hello, %s! 欢迎！唯一号:%d", name,
                response.getAccount_id());
        return new Greeting(time, content);
    }
}
